/**
 * Created by John on 2016/7/13.
 */
var MongoClient = require('mongodb').MongoClient;
var mongo = new MongoClient();

mongo.connect('mongodb://localhost/', function (err, db) {
    var myDB = db.db('BeginningMongoDB');
    myDB.collection('word_stats', function (err, collection) {
        includeFields(collection, ['word', 'size']);
        includeFields(collection, ['word', 'letters']);
        excludeFields(collection, ['letters', 'stats', 'charsets']);
        close(myDB);
    });
});

function includeFields(collection, fields) {
    var query = {first: 'p'};
    var fieldObj = {};
    for (var i in fields) {
        fieldObj[fields[i]] = true;
    }
    collection.findOne(query, fieldObj, function (err, item) {
        displayCursor(item, "Including " + fields + " fields:");
    });
}

function excludeFields(collection, fields) {
    var query = {first: 'p'};
    var fieldObj = {};
    for (var i in fields) {
        fieldObj[fields[i]] = false;
    }
    collection.findOne(query, fieldObj, function (err, item) {
        displayCursor(item, "Excluding " + fields + " fields:");
    });
}

function displayCursor(item, msg) {
    console.log(msg);
    console.log(item);
}

function close(myDB) {
    setTimeout(function () {
        myDB.close();
    }, 3000);
}